package cn.saas.jingtandata;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.DefaultAxisValueFormatter;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lecho.lib.hellocharts.formatter.AxisValueFormatter;
import lecho.lib.hellocharts.formatter.SimpleAxisValueFormatter;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.LineChartView;

public class HoleCptViewActivity extends AppCompatActivity implements
        OnClickListener, OnCheckedChangeListener {
    /**
     * 钻孔信息
     */
    public static final String INTENT_PARAM_HOLE = "hole_info";
    // ///////////////////////////////////////////
    /**
     * 曲线图
     */
    private LineChartView mLineChart;
    /**
     * 空数据
     */
    private LinearLayout mLayoutEmpty;
    /**
     * 纵向缩放开关
     */
    private Switch vZoomSwitch;
    /**
     * 横向缩放开关
     */
    private Switch hZoomSwitch;
    // ///////////////////////////////////////////

    /**
     * 钻孔GUID
     */
    private LpHoleInfoBean mHoleInfo;

    /**
     * 数据列表
     */
    private List<LpCptItemBean> mDataList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cpt_chart);
        initIntent();
        initView();
        initData();
    }

    /**
     * 初始化传入参数
     *
     * @Title: initIntent
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    private void initIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            Object obj = intent.getSerializableExtra(INTENT_PARAM_HOLE);
            if (obj instanceof LpHoleInfoBean) {
                mHoleInfo = (LpHoleInfoBean) obj;
            }
        }
    }

    /**
     * 初始化视图
     *
     * @Title: initView
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    private void initView() {
        setTitle("静探曲线");
        mLineChart = (LineChartView) findViewById(R.id.chart);
        mLayoutEmpty = (LinearLayout) findViewById(R.id.empty_layout);
        vZoomSwitch = (Switch) findViewById(R.id.v_switch);
        vZoomSwitch.setOnCheckedChangeListener(this);
        hZoomSwitch = (Switch) findViewById(R.id.h_switch);
        hZoomSwitch.setOnCheckedChangeListener(this);
    }

    /**
     * 初始化数据
     *
     * @Title: initData
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    private void initData() {
        Bundle bundle = getIntent().getExtras();
        mDataList = (List<LpCptItemBean>) bundle.getSerializable("data");

        if (mDataList != null && mDataList.size() > 0) {
            initLineChar(mDataList);
            mLayoutEmpty.setVisibility(View.GONE);
            mLineChart.setVisibility(View.VISIBLE);
        } else {
            mLayoutEmpty.setVisibility(View.VISIBLE);
            mLineChart.setVisibility(View.GONE);
        }
    }


    /**
     * 初始化图标
     *
     * @param data
     * @Title: initLineChar
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */
    private void initLineChar(List<LpCptItemBean> data) {
        // 曲线
        List<Line> lines = new ArrayList<Line>();
        // 侧壁阻力
        List<PointValue> cbzl = new ArrayList<PointValue>();
        // 锥尖阻力
        List<PointValue> zjzl = new ArrayList<PointValue>();
        // 整理数据
        List<String> yLabList = new ArrayList<String>();

        //加入起始节点
        cbzl.add(new PointValue(0, 0));
        zjzl.add(new PointValue(0, 0));

        // 整理数据，按照数据深度倒序处理
        List<Float> depTmpList = new ArrayList<Float>();
        List<Double> topList = new ArrayList<Double>();
        List<Double> cbList = new ArrayList<Double>();
        for (LpCptItemBean lpCptItemBean : data) {
            if (lpCptItemBean != null) {
                float depth = (float) lpCptItemBean.getCbDepth();
                // 提取整数lable,已负数模式，实现倒叙
                if (depth == (int) depth) {
                    yLabList.add((int) depth + "m");
                    depTmpList.add((float) -depth);
                }
                cbzl.add(new PointValue(
                        (float) lpCptItemBean.getCbCBZL() * 100, -depth));
                zjzl.add(new PointValue((float) lpCptItemBean.getCbZJZL(),
                        -depth));
                // 获取锥尖阻力
                topList.add(lpCptItemBean.getCbZJZL());
                cbList.add(lpCptItemBean.getCbCBZL());
            }
        }
        getAxisXLables(cbzl);//获取x轴的标注

        depTmpList.add(0f);
        yLabList.add("0m");

        // 申明曲线
        Line zjzlLine = new Line(zjzl);
        zjzlLine.setColor(Color.GRAY);
        zjzlLine.setHasPoints(false);
        zjzlLine.setHasLabels(false);
        zjzlLine.setStrokeWidth(2);
        lines.add(zjzlLine);

        Line cbzlLine = new Line(cbzl);
        cbzlLine.setColor(ChartUtils.COLOR_GREEN);
        cbzlLine.setHasLabels(false);
        cbzlLine.setHasPoints(false);
        cbzlLine.setStrokeWidth(1);
        lines.add(cbzlLine);
        // 申明曲线图
        LineChartData lineChartData = new LineChartData(lines);

        Axis xTop = new Axis();
        xTop.setName("锥尖阻力(灰)");
        xTop.setTextColor(Color.GRAY);
        xTop.setMaxLabelChars(4);
        xTop.setFormatter(new SimpleAxisValueFormatter().setAppendedText("MPa"
                .toCharArray()));
        xTop.setHasLines(true);
        xTop.setInside(false);
        lineChartData.setAxisXTop(xTop);

        /*Axis xButtom = new Axis();
        xButtom.setName("侧壁阻力(绿)");
        xButtom.setTextColor(ChartUtils.COLOR_GREEN);
        xButtom.setMaxLabelChars(4);
        xButtom.setFormatter(new SimpleAxisValueFormatter()
                .setAppendedText("0kPa".toCharArray()));
        xButtom.setHasLines(true);
        xButtom.setInside(false);
        lineChartData.setAxisXBottom(xButtom);*/
        lineChartData.setAxisXBottom(new Axis().setName("侧壁阻力(绿)").setMaxLabelChars(4).setTextColor(ChartUtils.COLOR_GREEN)
                .setFormatter(new HeightValueFormatter(1, 1, 0).setAppendedText("kpa".toCharArray())).setInside(false));


        Axis yLeft = Axis.generateAxisFromCollection(depTmpList, yLabList);
        // Axis yLeft = new Axis();
        // yLeft.setName("深度");
        yLeft.setTextColor(ChartUtils.COLOR_BLUE);
        yLeft.setMaxLabelChars(8);
        yLeft.setFormatter(new SimpleAxisValueFormatter().setAppendedText("m"
                .toCharArray()));
        yLeft.setHasLines(true);
        yLeft.setAutoGenerated(false);
        yLeft.setInside(false);

        lineChartData.setAxisYLeft(yLeft);
        // Set data
        mLineChart.setLineChartData(lineChartData);
        mLineChart.setScrollEnabled(true);
        mLineChart.setZoomType(ZoomType.HORIZONTAL);
    }

    private void getAxisXLables(List<PointValue> cbzl) {
        List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();
        for (int i = 0; i < cbzl.size(); i++) {
            mAxisXValues.add(new AxisValue(i).setLabel((cbzl.get(i).getY() * 10) + "Mpa"));
        }
    }


    /**
     * 静探导入 .
     */
    private static final int INTENT_CPT_IMPORT = 1001;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
           /* case R.id.txt_btn:
                Intent kmlIntent = new Intent(this, HoleCPTImportActivity.class);
                kmlIntent.putExtra(HoleCPTImportActivity.INTENT_PARAM_HOLE,
                        mHoleInfo);
                startActivityForResult(kmlIntent, INTENT_CPT_IMPORT);
                break;*/
        }

    }

    @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        super.onActivityResult(arg0, arg1, arg2);
        switch (arg0) {
            case INTENT_CPT_IMPORT:
                if (arg1 == RESULT_OK) {
                    initData();
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        ZoomType zoomType = null;
        if (vZoomSwitch.isChecked()) {
            zoomType = ZoomType.VERTICAL;
        }
        if (hZoomSwitch.isChecked()) {
            if (zoomType == null) {
                zoomType = ZoomType.HORIZONTAL;
            } else {
                zoomType = ZoomType.HORIZONTAL_AND_VERTICAL;
            }
        }
        if (zoomType == null) {
            mLineChart.setZoomEnabled(false);
        } else {
            mLineChart.setZoomEnabled(true);
            mLineChart.setZoomType(zoomType);
        }
    }

    private static class HeightValueFormatter extends SimpleAxisValueFormatter {

        private float scale;
        private float sub;
        private int decimalDigits;

        public HeightValueFormatter(float scale, float sub, int decimalDigits) {
            this.scale = scale;
            this.sub = sub;
            this.decimalDigits = decimalDigits;
        }

        @Override
        public int formatValueForAutoGeneratedAxis(char[] formattedValue, float value, int autoDecimalDigits) {
            float scaledValue = (value + 0) *10;
            Log.e("aaaaa", "formatValueForAutoGeneratedAxis: " + scaledValue);
            return super.formatValueForAutoGeneratedAxis(formattedValue, scaledValue, this.decimalDigits);
        }
    }
}
